#include <Encabulator.h>
#include <Wire.h>
// #include <I2C.h>   // NOTE: For reasons buried in the Encabulator.cpp file, you might want to download and use this library instead. 

int loopCounter = 0;

int tempR = 0;
int tempG = 0;
int tempB = 0;

float tempX = 0.0;
float tempY = 0.0;

int palindrome = 0; 

void setup() {               

  // secret startup sequence 
  
  Encabulator.upUpDownDownLeftRightLeftRightBA();

  // enable some debugging on the serial port

  Encabulator.setVerbose(true);

  // check the temperature
  
  float weatherReport = Encabulator.temperature.getFahrenheit();
  Serial.print("Currrent operating temperature is: ");
  Serial.print(weatherReport);
  Serial.println("F");

  // check EEPROM   

  for (uint16_t i = 0 ; i < 52 ; i++) {
    Serial.print((char) Encabulator.eeprom.readAddress(i));
  }  
  Serial.println();

  // check the DAC

  Encabulator.dac.write(100);

  // check the XYZ

  int x = Encabulator.accelerometer.x();
  int y = Encabulator.accelerometer.y();
  int z = Encabulator.accelerometer.z();

  Serial.print("Heads or Tails?");

  if (z < 0.0) {
    Serial.println("  ...Heads!");
  } else {
    Serial.println("  ...Tails!");
  }
  
  // finish startup sequence with two seconds of global test pattern
  
  Encabulator.testPattern();
  
  delay(1000);
  
  Encabulator.blackout();
  
  delay(500);

  Encabulator.addressable.blackout();
  Encabulator.addressable.drawGradient(random() % 255,random() % 255,0,0,random() % 255,random() % 255,10);

  delay(500);

  // fade up to white

  for (int i = 1 ; i < 5 ; i++) {  
    Encabulator.stripBankA.fadeHeaderToRGB(i,255,255,255,10); 
    Encabulator.stripBankB.fadeHeaderToRGB(5-i,255,255,255,10);
    delay(500);
  }
    
}

void loop() {

  palindrome = (loopCounter % 510 > 255) ? 255 - (loopCounter % 510) : loopCounter % 510;
  
  tempX = Encabulator.accelerometer.x() / 1024.0;

  // do a color pattern across the 8 headers based roughly on the X axis

  Encabulator.stripBankA.jumpHeaderToRGB((uint8_t) 1,(uint8_t) palindrome * tempX,0,(uint8_t) (255 - palindrome) * tempX); 
  Encabulator.stripBankA.jumpHeaderToRGB((uint8_t) 2,(uint8_t) (255 - palindrome) * tempX,0,(uint8_t) palindrome * tempX); 
  Encabulator.stripBankA.jumpHeaderToRGB((uint8_t) 3,(uint8_t) palindrome * tempX,0,(uint8_t) (255 - palindrome) * tempX); 
  Encabulator.stripBankA.jumpHeaderToRGB((uint8_t) 4,(uint8_t) (255 - palindrome) * tempX,0,(uint8_t) palindrome * tempX); 

  Encabulator.stripBankA.jumpHeaderToRGB(1,(uint8_t) (255 - palindrome) * tempX,0,(uint8_t) palindrome * tempX); 
  Encabulator.stripBankA.jumpHeaderToRGB(2,(uint8_t) palindrome * tempX,0,(uint8_t) (255 - palindrome) * tempX); 
  Encabulator.stripBankA.jumpHeaderToRGB(3,(uint8_t) (255 - palindrome) * tempX,0,(uint8_t) palindrome * tempX); 
  Encabulator.stripBankA.jumpHeaderToRGB(4,(uint8_t) palindrome * tempX,0,(uint8_t) (255 - palindrome) * tempX); 
 
  // do a gradient with its length based roughly on the Y axis tilt

  tempY = Encabulator.accelerometer.y() / 1024.0;

  int tempLength = 64; // (int) (30.0 + (30.0 * (tempY)));

  Encabulator.addressable.drawGradient((uint8_t) 255 * tempY,0,(uint8_t) 255 * tempX,0,0,255,64);
  
  // do a fade on DMX address 1
    
  Encabulator.dmx.setDataStartingAtAddressWithRGB(1,loopCounter % 255,0,255 - (loopCounter % 255));  
  
  // increment loop
  
  loopCounter++;
 
}
